package com.example.demomap;

import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXLoader.ITMXTilePropertiesListener;
import org.andengine.extension.tmx.TMXProperties;
import org.andengine.extension.tmx.TMXTile;
import org.andengine.extension.tmx.TMXTileProperty;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.opengl.GLES20;
import android.util.DisplayMetrics;
import android.widget.Toast;

public class DemoTMX extends SimpleBaseGameActivity {

	// ===========================================================
	// Constants
	// ===========================================================

	private static int CAMERA_WIDTH = 800;
	private static int CAMERA_HEIGHT = 600;

	// ===========================================================
	// Fields
	// ===========================================================

	private BoundCamera mCamera;

	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mPlayerTextureRegion;

	private BitmapTextureAtlas mOnScreenControlTexture;
	private ITextureRegion mOnScreenControlBaseTextureRegion;
	private ITextureRegion mOnScreenControlKnobTextureRegion;
	
	private int cameraWidth=0;
	private int cameraHeigh=0;

	private DigitalOnScreenControl mDigitalOnScreenControl;
	private int direction;
	private TMXTiledMap mTMXTiledMap;
	protected int mCactusCount = 0;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public EngineOptions onCreateEngineOptions() {
		final DisplayMetrics displayMetrics = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		CAMERA_WIDTH = displayMetrics.widthPixels;
		CAMERA_HEIGHT = displayMetrics.heightPixels;
		this.mCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
				new FillResolutionPolicy(), this.mCamera);
	}

	@Override
	public void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		this.mBitmapTextureAtlas = new BitmapTextureAtlas(
				this.getTextureManager(), 400, 400, TextureOptions.BILINEAR);
		this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(this.mBitmapTextureAtlas, this,
						"player1.png", 0, 0, 4, 4);
		this.mBitmapTextureAtlas.load();

		this.mOnScreenControlTexture = new BitmapTextureAtlas(
				this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
		this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mOnScreenControlTexture, this,
						"onscreen_control_base.png", 0, 0);
		this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mOnScreenControlTexture, this,
						"onscreen_control_knob.png", 128, 0);
		this.mOnScreenControlTexture.load();
	}

	@Override
	public Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();

		final TMXLoader tmxLoader = new TMXLoader(this.getAssets(),
				this.mEngine.getTextureManager(), TextureOptions.DEFAULT,
				this.getVertexBufferObjectManager());
		try {
			this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/maptest.tmx");
		} catch (TMXLoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++) {
				if(i==0)
				{
					TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(i);
					cameraHeigh = tmxLayer.getHeight();
					cameraWidth = tmxLayer.getWidth();
					scene.attachChild(tmxLayer);
				}
				else
					scene.attachChild(this.mTMXTiledMap.getTMXLayers().get(i));
			}

		/* Make the camera not exceed the bounds of the TMXEntity. */
		this.mCamera.setBounds(0, 0, cameraHeigh, cameraWidth);
		this.mCamera.setBoundsEnabled(true);
		final float centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion
				.getWidth()) / 2;
		final float centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion
				.getHeight()) / 2;
		final AnimatedSprite player = new AnimatedSprite(centerX, centerY,
				this.mPlayerTextureRegion, this.getVertexBufferObjectManager());
		this.mCamera.setChaseEntity(player);
		final PhysicsHandler physicsHandler = new PhysicsHandler(player);
		player.registerUpdateHandler(physicsHandler);

		scene.attachChild(player);
		direction = 99;

		this.mDigitalOnScreenControl = new DigitalOnScreenControl(0,
				CAMERA_HEIGHT
						- this.mOnScreenControlBaseTextureRegion.getHeight(),
				this.mCamera, this.mOnScreenControlBaseTextureRegion,
				this.mOnScreenControlKnobTextureRegion, 0.1f,
				this.getVertexBufferObjectManager(),
				new IOnScreenControlListener() {
					@Override
					public void onControlChange(
							final BaseOnScreenControl pBaseOnScreenControl,
							final float pValueX, final float pValueY) {
						if (pValueX > 0) {
							// X = 1 => Right
							if (direction != 8) {
								player.animate(
										new long[] { 100, 100, 100, 100 }, 8,
										11, true);
								direction = 8;
							}
						} else if (pValueX < 0) {
							// X = -1 => Left
							if (direction != 4) {
								player.animate(
										new long[] { 100, 100, 100, 100 }, 4,
										7, true);
								direction = 4;
							}
						} 
//						else if (pValueY > 0) {
//							// Y = 1 => Down
//							if (direction != 0) {
//								player.animate(
//										new long[] { 100, 100, 100, 100 }, 0,
//										3, true);
//								direction = 0;
//							}
//						} else if (pValueY < 0) {
//							// Y = -1 => Up
//							if (direction != 12) {
//								player.animate(
//										new long[] { 100, 100, 100, 100 }, 12,
//										15, true);
//								direction = 12;
//							}
//						} 
						else {
							if (direction != 99)
								player.stopAnimation(direction);
							direction = 99;
						}
						if(direction==4 || direction==8)
							physicsHandler
								.setVelocity(pValueX * 100, pValueY * 100);
						else 
							physicsHandler
							.setVelocity(0,0);
					}
				});
		this.mDigitalOnScreenControl.getControlBase().setBlendFunction(
				GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
		this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
		this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
		this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
		this.mDigitalOnScreenControl.refreshControlKnobPosition();

		scene.setChildScene(this.mDigitalOnScreenControl);

		return scene;
	}

	@Override
	public void onGameCreated() {
	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
